package com.tg.base.concurrent.lock.reentrantReadWriteLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by MagicBook on 2020/1/31.
 */
public class Account {
    private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
    private final Lock readLock = rwl.readLock();    //读锁
    private final Lock writeLock = rwl.writeLock();    //写锁

    private Integer amount;

    public Integer getAmount() {
        readLock.lock();
        try {
            return amount;
        } finally {
            readLock.unlock();
        }
    }

    public Integer updateAmount(Integer a) {
        writeLock.lock();
        try {
            this.amount = a + (this.amount == null ? 0 : this.amount);
            return this.amount;
        } finally {
            writeLock.unlock();
        }
    }
}
